package com.lvmama.core.bean.base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页对象
 * 
 * @author caoyong
 * @since 2018年6月29日 上午11:21:55
 * @param <E>
 */

public class Page<E> implements Serializable {
    private static final long serialVersionUID = 2647885913495907787L;

    /**
     * pageSize
     */
    private Integer           limit            = 10;

    /**
     * 开始行
     */
    private Integer           start            = 0;

    /**
     * 总记录数
     */
    private Integer           results          = 0;

    /**
     * 对象参数
     */
    private E                 params;

    /**
     * 列表参数
     */
    private List<E>           rows             = new ArrayList<>();

    /**
     * 是否成功
     */
    private Boolean           isSuccess        = false;

    /**
     * 错误信息
     */
    private String            error;

    /**
     * 错误代码
     */
    private String            errorCode;

    /**
     * 是否分页
     */
    private Boolean           page;

    /**
     * 第几页
     */
    private Integer           pageNo           = 1;

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Integer getStart() {
        return start;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    public Integer getResults() {
        return results;
    }

    public void setResults(Integer results) {
        this.results = results;
    }

    public E getParams() {
        return params;
    }

    public void setParams(E params) {
        this.params = params;
    }

    public List<E> getRows() {
        return rows;
    }

    public void setRows(List<E> rows) {
        this.rows = rows;
    }

    public Boolean getIsSuccess() {
        return isSuccess;
    }

    public void setIsSuccess(Boolean isSuccess) {
        this.isSuccess = isSuccess;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public Boolean getPage() {
        return page;
    }

    public void setPage(Boolean page) {
        this.page = page;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getTotalPage() {
        Integer totalPage = this.results / this.limit;
        if ((totalPage == 0) || (this.results % this.limit != 0)) {
            totalPage++;
        }
        return totalPage;
    }

    /**
     * rows最多显示前10条
     */
    @Override
    public String toString() {
        final Integer maxLen = 10;
        return "Page [limit=" + limit + ", start=" + start + ", results=" + results + ", params=" + params + ", rows="
                + (null != rows ? rows.subList(0, Math.min(rows.size(), maxLen)) : null) + ", isSuccess=" + isSuccess
                + ", error=" + error + ", errorCode=" + errorCode + ", page=" + page + "]";
    }

}
